using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;

namespace FrontDisplay
{
    internal sealed class PluginParametersCollection : IEnumerable<PluginParameters>
    {
        private List<PluginParameters> _list = new List<PluginParameters>();

        public PluginParametersCollection(IEnumerable<IPlugin> plugins, ItemsControl tabControl)
        {
            foreach (IPlugin plugin in plugins)
            {
                UIElement parametersPage = plugin.ParametersControl;
                if (parametersPage != null)
                {
                    var tabItem = new PluginParameters(plugin, parametersPage);
                    _list.Add(tabItem);
                    tabControl.Items.Add(tabItem.TabItem);
                }
            }

            Def.Localization.CultureChanged += LocalizationCultureChanged;
        }

        private void LocalizationCultureChanged(CultureInfo cultureInfo)
        {
            _list.ForEach(item => item.UpdateTitle());
        }

        public IEnumerator<PluginParameters> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void SaveConfig()
        {
            _list.ForEach(item => item.SaveConfig());
        }
    }
}